import 'package:sow_app_i18n/sow_app_i18n.dart';

import 'usecase.dart';

import '../presenter/setting_presenter.dart';
import '../repository/cache_repository.dart';
import '../repository/rest_client.dart';
import '../repository/token_repository.dart';
import '../usecase/cache_usecase.dart';
import '../usecase/token_usecase.dart';

class SettingValidateClientUseCase extends UseCase<void, String> {
  final SettingPresenter presenter;

  final CacheRepository _cacheRepository;
  final TokenRepository _tokenRepository;
  final RestClient _client;

  SettingValidateClientUseCase(this.presenter, this._cacheRepository,
      this._tokenRepository, this._client);
  @override
  void call(String params) async {
    try {
      await presenter.client.getFile(I18n.base_cover_url);
      await SetCacheUseCase(_cacheRepository)(SetCacheParams(
          I18n.ajax_info, "${presenter.host},${presenter.port}"));

      await SetTokenUseCase(_tokenRepository, _client)(params);
      presenter.client.setAuthorization(params);
      presenter.connectionValid = true;
      presenter.onComplete();
    } on Exception catch (e) {
      presenter.onError(e);
    }
  }
}
